﻿using FinalPointSet.Model.Manager;
using System.Collections.Generic;
using System.Linq;

namespace FinalPointSet.Model.Model.FieldMapping
{
    public class DataSourceMappingHelper
    {
        /// <summary>
        /// 初始化数据源映射
        /// </summary>
        /// <returns></returns>
        public static List<DataSourceMapping> GetDataSourceMappings()
        {
            List<DataSource> dataSources = AssetManager.Instance.GetJsonObjectList<DataSource>();
            List<DataSourceMapping> dsMappings = AssetManager.Instance.GetJsonObjectList<DataSourceMapping>();
            List<DataSourceMapping> totalDataSourceMappings = [];
            //双重循环,两两组合生成所有数据源组合
            foreach (DataSource firstDS in dataSources)
            {
                //初始化一级节点
                DataSourceMapping dsMapping = new()
                {
                    Sequence = firstDS.Sequence,
                    SourceDataSource = firstDS
                };
                foreach (DataSource secondDS in dataSources)
                {
                    //跳过自身节点
                    if (firstDS.Equals(secondDS))
                        continue;
                    DataSourceMappingRule? exsitDataSourceMappingRule = FindDataSourceMappingRule(dsMappings, firstDS, secondDS);
                    List<FieldMappingKeyValuePair> fieldMappings = exsitDataSourceMappingRule?.FieldTypeMapping?.ToList() ?? [];
                    //初始化二级节点
                    DataSourceMappingRule dsMappingRule = new()
                    {
                        Sequence = secondDS.Sequence,
                        SourceDataSource = firstDS,
                        TargetDataSource = secondDS,
                    };
                    //初始化字段映射规则
                    List<DataSourceFieldType> dsFieldTypes = [.. firstDS.FieldTypes];
                    List<FieldMappingKeyValuePair> genFieldMappings = [];
                    foreach (DataSourceFieldType fieldType in dsFieldTypes)
                    {
                        FieldMappingKeyValuePair kp = fieldMappings.FirstOrDefault(e => fieldType.Name.Equals(e.SourceFieldType), new()) ?? new();
                        if (string.IsNullOrEmpty(kp.SourceFieldType))
                        {
                            kp.SourceFieldType = fieldType.Name;
                            kp.Sequence = fieldType.Sequence;
                        }
                        genFieldMappings.Add(kp);
                    }
                    dsMappingRule.FieldTypeMapping = new(genFieldMappings);
                    dsMapping.MappingRules.Add(dsMappingRule);
                }
                totalDataSourceMappings.Add(dsMapping);
            }

            return totalDataSourceMappings;
        }

        /// <summary>
        /// 返回指定来源&目标数据源的映射规则
        /// </summary>
        /// <param name="dataSourceMappings">映射规则集合</param>
        /// <param name="sourceDataSource">来源数据源</param>
        /// <param name="targetDataSource">目标数据源</param>
        /// <returns></returns>
        public static DataSourceMappingRule? FindDataSourceMappingRule(List<DataSourceMapping> dataSourceMappings, DataSource sourceDataSource, DataSource targetDataSource)
        {
            DataSourceMapping? dsMapping = dataSourceMappings.FirstOrDefault(dsm => dsm.SourceDataSource.Name.Equals(sourceDataSource.Name));
            if (dsMapping == null)
            {
                return null;
            }
            return dsMapping.MappingRules.FirstOrDefault(mr => mr.TargetDataSource.Name.Equals(targetDataSource.Name));
        }

        /// <summary>
        /// 返回指定来源&目标数据源的映射规则
        /// </summary>
        /// <param name="sourceDataSource">来源数据源</param>
        /// <param name="targetDataSource">目标数据源</param>
        /// <returns></returns>
        public static DataSourceMappingRule? FindDataSourceMappingRule(DataSource sourceDataSource, DataSource targetDataSource)
        {
            return FindDataSourceMappingRule(GetDataSourceMappings(), sourceDataSource, targetDataSource);
        }


        /// <summary>
        /// 以数据源作为全集,生成模板集合(处理数据源变动的场景)
        /// </summary>
        /// <param name="dataSources">数据源集合</param>
        /// <param name="formatTemplates">原模板集合</param>
        /// <returns></returns>
        public static List<FormatTemplate> GetFormatTemplates()
        {
            List<FormatTemplate> newFormatTemplate = [];
            List<DataSource> dataSources = AssetManager.Instance.GetJsonObjectList<DataSource>();
            List<FormatTemplate> formatTemplates = AssetManager.Instance.GetJsonObjectList<FormatTemplate>();
            foreach (DataSource dataSource in dataSources)
            {
                FormatTemplate formatTemplate = formatTemplates.FirstOrDefault(ds => ds.DataSource.Name.Equals(dataSource.Name), new FormatTemplate() { DataSource = dataSource });
                formatTemplate.DataSource.Sequence = dataSource.Sequence;
                formatTemplate.DataSource.LogoPath = dataSource.LogoPath;
                newFormatTemplate.Add(formatTemplate);
            }
            return new List<FormatTemplate>(newFormatTemplate);
        }

        /// <summary>
        /// 获取目标数据源的生成模板
        /// </summary>
        /// <param name="targetDataSource"></param>
        /// <returns></returns>
        public static FormatTemplate? FindFormatTemplate(DataSource targetDataSource)
        {
            List<FormatTemplate> formatTemplates = GetFormatTemplates();
            return formatTemplates.FirstOrDefault(tp => targetDataSource.Name.Equals(tp.DataSource.Name));
        }
    }
}
